(function () {
    //{if $prev}//var appid = 'wx2054bed22814a315',secret = '77305888f781790605ca7f1fd2eceaf6',
    //{else}//
    var appid = 'wx0e1c69a45f2a6c5c',
        secret = '5bb03649bf848c4b398f1ef4e7ea69b9',
    //{/if}//
        //{if 1}//code = location.href.split('?')[1].split('&').map(function (item) { return item.slice(0, 5) === 'code=' ? item.replace('code=', '') : ''; }).join(''),//{/if}//
        openid = localStorage.getItem('openId');

    ecui.esr.headers = {
        'x-client': 'WeChat',
        //{if 1}//'x-open-id': openid
        //{else}//
        'x-open-id': 'o6UsLuC7NlsQdBLGim9rbsnh9Uas'
        //{/if}//
    };
    // 微信入口链接：        https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2054bed22814a315&redirect_uri=http%3A%2F%2Fu.mao-livehouse.com/maolive-app/bind.html&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
    // 测试环境微信入口链接： https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx0e1c69a45f2a6c5c&redirect_uri=http%3A%2F%2Ft.yinyueapp.com/maolive-app/bind.html&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
    ecui.esr.onready = function () {
    };

    /**
    * 根据基础字体大小 和 效果图宽度设置 根节点字体大小
    * @param baseFontSize 浏览器默认字体大小 [注]浏览器最小字体为12像素，所以该值必需大于等于12
    * @param baseWidth 设计稿的尺寸
    */
    function initFontSize(baseFontSize, baseWidth) {
        // 获取当前屏幕宽度
        var clientWidth = document.documentElement.clientWidth || window.innerWidth;
        // 根据宽度计算根节点字体大小
        var size =  clientWidth / baseWidth * baseFontSize;
        document.querySelector('html').style.fontSize = size + 'px';
    }
    //初始化 字体100px , 设计稿宽640px
    initFontSize(75, 750);
    // 窗口大小改变时设置根节点字体大小，通过字体大小来控制页面元素尺寸的缩放
    window.addEventListener('resize', initFontSize);

    window.onload = function () {
        var intervalId = setInterval(function () {
            if (ecui.getNamedControls()['bind-btn']) {
                clearInterval(intervalId);
                if (openid) {
                    document.forms.bindForm.elements.openId.getControl().setValue(openid);
                } else {
                    //{if 1}//ecui.get('bind-btn').disable();//{/if}//
                    ecui.io.ajax(
                        // 微信入口链接：https://api.weixin.qq.com/sns/oauth2/access_token?appid=' + appid + '&secret=' + secret + '&code=' + code + '&grant_type=authorization_code',
                        '/erp-management/weixin/openid?code=' + code,
                        {
                            header: {
                                'x-client': 'WeChat'
                            },
                            method: 'get',
                            onsuccess: function (data) {
                                data = JSON.parse(data);
                                if (data.code === 0) {
                                    openid = data.data.openid;
                                    ecui.esr.headers['x-open-id'] = openid;
                                    localStorage.setItem('openId', openid);
                                    document.forms.bindForm.elements.openId.getControl().setValue(openid);
                                    ecui.get('bind-btn').enable();
                                } else {
                                    ecui.tip('warn', data.msg);
                                }
                            },
                            onerror: function (code, msg) {
                                ecui.tip('warn', msg);
                            }
                        }
                    );
                    //{if 0}//
                    document.forms.bindForm.elements.openId.getControl().setValue('o6UsLuC7NlsQdBLGim9rbsnh9Uas');
                    //{/if}//
                }
                var bind = function () {
                    ecui.esr.request(
                        'data@FORM /erp-management/bind/account?bindForm',
                        function () {
                            var data = ecui.esr.getData('data_CODE');
                            if (data === 0) {
                                // location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=http%3A%2F%2Fu.mao-livehouse.com/maolive-app/index.html%23/venueOrder/calendar&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
                                location.href = 'index.html';
                            }
                        },
                        function () {

                        }
                    );
                };
                ecui.get('bind-btn').onclick = bind;
            }
        }, 10); //以10毫秒位单位重新调整setInterval函数的匿名函数。
    };
}());

